package com.lq.ecoldchain.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.lq.ecoldchain.base.BaseNavigator;
import com.lq.ecoldchain.config.AppConfig;
import com.lq.ecoldchain.entity.wechat.WxResponseBean;
import com.lq.ecoldchain.model.WeChatModel;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    /**
     * 微信登录相关
     */
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //通过WXAPIFactory工厂获取IWXApI的示例
        api = WXAPIFactory.createWXAPI(this, AppConfig.WX_KEY_APP_ID, true);
        //将应用的appid注册到微信
        api.registerApp(AppConfig.WX_KEY_APP_ID);
        api.handleIntent(this.getIntent(), this);
        Log.e("WXEntryActivity", "------------------------------------");
        //注意：
        //第三方开发者如果使用透明界面来实现WXEntryActivity，需要判断handleIntent的返回值，如果返回值为false，则说明入参不合法未被SDK处理，应finish当前透明界面，避免外部通过传递非法参数的Intent导致停留在透明界面，引起用户的疑惑
        try {
            boolean result = api.handleIntent(getIntent(), this);
            if (!result) {
                Log.e("WXEntryActivity", "参数不合法，未被SDK处理，退出");
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        api.handleIntent(data, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.e("WXEntryActivity", "baseReq:" + new Gson().toJson(baseReq));
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.e("WXEntryActivity", "baseResp:toJson:" + new Gson().toJson(baseResp));
        String result = "";
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                result = "发送成功";
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = "发送取消";

                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = "发送被拒绝";

                break;
            default:
                result = "发送返回";
                break;

        }
        Log.e("WXEntryActivity", "result:" + result);
        WxResponseBean entity = new Gson().fromJson(new Gson().toJson(baseResp), WxResponseBean.class);
        String code = entity.getCode();
        WeChatModel.INSTANCE.loginAccessToken(code);
//        Fragment fragment = (Fragment) ARouter.getInstance().build(BaseNavigator.loginFragment).withString("code", code).navigation();
//		Intent intent = new Intent(this, SignInActivity.class);
//		intent.putExtra(RESULT,new Gson().fromJson(new Gson().toJson(baseResp), WXCodeBean.class).getCode());
//		startActivity(intent);
        finish();

    }
}